<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="formRef" :model="form" :rules="rules" class="mr20" label-width="90px">
        <el-divider content-position="left" style="margin-top: -5px;"><span class="divider-css">▶基本信息◀</span></el-divider>
        <el-row>
          <el-col :span="4">
            <el-form-item label="患者姓名" prop="name">
              <span class="ys-css">{{ form.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option v-for="dict in dictDataMap['sys_user_sex']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item v-if="form.newbornFlag === 'N'" label="职业" prop="occupation">
              <el-select v-model="form.occupation" placeholder="请选择职业" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['profession_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-else label="出生日期" prop="birthdayDate">
              <el-date-picker clearable v-model="form.birthdayDate" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择生日" @change="birthdayDateChange">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-row>
              <el-col :span="4" class="age-margin">
                <el-form-item label="年龄" prop="age">
                  <el-input v-model="form.age" placeholder="岁" clearable class="age" ref="ageRef">
                    <template #append>岁</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4" class="age-margin">
                <el-form-item prop="ageMonth">
                  <el-input v-model="form.ageMonth" placeholder="月" clearable class="age">
                    <template #append>月</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4" class="age-margin">
                <el-form-item prop="ageDay">
                  <el-input v-model="form.ageDay" placeholder="天" clearable class="age">
                    <template #append>天</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4" class="age-margin">
                <el-button @click="resetAge" type="primary" style="position: absolute;right: 150px;" plain>
                  清空
                </el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="身份证号" prop="idNo">
              <el-input v-model="form.idNo" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="国籍" prop="nationality">
              <el-select v-model="form.nationality" placeholder="请选择国籍" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['nationality_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="民族" prop="nation">
              <el-select v-model="form.nation" placeholder="请选择民族" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['nation_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.newbornFlag === 'N'">
            <el-form-item label="工作单位" prop="workContract">
              <el-input v-model="form.workContract" placeholder="请输入工作单位" clearable />
            </el-form-item>
          </el-col>
          <template v-else>
            <el-col :span="6">
              <el-row>
                <el-col :span="8" class="age-margin">
                  <el-form-item prop="ageHours" label="出生时刻">
                    <el-input v-model="form.ageHours" placeholder="时" clearable class="age">
                      <template #append>时</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="1" class="age-margin">
                  <el-form-item prop="ageMinutes">
                    <el-input v-model="form.ageMinutes" placeholder="分" clearable class="age">
                      <template #append>分</template>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </template>
          <el-col :span="4">
            <el-form-item label="婚姻" prop="marriage">
              <el-select v-model="form.marriage" placeholder="请选择婚姻" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['marriage_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="籍贯" prop="nativePlace">
              <el-input v-model="form.nativePlace" placeholder="请输入籍贯" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生地" prop="homePlace">
              <el-input v-model="form.homePlace" placeholder="请输入出生地" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="住址" prop="address">
              <el-input v-model="form.address" placeholder="请输入住址" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="邮编" prop="zipCode">
              <el-input v-model="form.zipCode" placeholder="请输入邮编" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="文化程度" prop="eduLevel">
              <el-select v-model="form.eduLevel" placeholder="请选择文化程度" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['education_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left"><span class="divider-css">▶入院信息◀</span></el-divider>
        <el-row>
          <el-col :span="4">
            <el-form-item label="入院时间" prop="nativePlace">
              <el-date-picker clearable v-model="form.createTime" type="datetime" placeholder="请选择发病时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入院科室" prop="deptAdmissionTo">
              <dept-list style="width: 100%" :clinic-vs-visit-str="'1,2'" v-model="form.deptAdmissionTo"
                :dept-id="form.deptAdmissionTo" :org-id="userInfo.orgId" :placeholder="'请选择入院科室'"
                @deptChange="deptChange" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入院病区" prop="wardAdmissionTo">
              <dept-vs-bed-list style="width: 100%" :clinical-sign-str="'3'" v-model="form.wardAdmissionTo"
                :dept-id="form.wardAdmissionTo" :org-id="userInfo.orgId" :placeholder="'请选择入院病区'"
                @deptChange="wordChange" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入院情况" prop="hospitalSituation">
              <el-select v-model="form.hospitalSituation" placeholder="请选择入院情况">
                <el-option v-for="dict in dictDataMap['admission_situation_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="费别" prop="chargeType">
              <el-select v-model="form.chargeType" placeholder="请选择费别">
                <el-option v-for="dict in dictDataMap['medical_insurance_type']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入院方式" prop="channel">
              <el-select v-model="form.channel" placeholder="请选择入院方式">
                <el-option v-for="dict in dictDataMap['patient_class_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系人身份证号" prop="contactsIdNo">
              <el-input v-model="form.contactsIdNo" placeholder="请输入联系人身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系人地址" prop="contactsAddress">
              <el-input v-model="form.contactsAddress" placeholder="请输入联系人地址" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系人电话" prop="contactsPhone">
              <el-input v-model="form.contactsPhone" placeholder="请输入联系人电话" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系人关系" prop="contactsRelation">
              <el-select v-model="form.contactsRelation" placeholder="请选择联系人关系">
                <el-option v-for="dict in dictDataMap['relationship_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="隔离情况">
              <el-radio-group v-model="form.quarantine">
                <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue" :value="dict.dictValue">{{
                  dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left"><span class="divider-css">▶其他信息◀</span></el-divider>
        <el-row>
          <el-col :span="4">
            <el-form-item label="参保类型" prop="canBaoType">
              <el-select v-model="form.canBaoType" placeholder="请选择参保类型">
                <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="医保类别" prop="insuranceType">
              <el-select v-model="form.insuranceType" placeholder="请选择医保类别">
                <el-option v-for="dict in dictDataMap['medical_insurance_type']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="医保卡号" prop="insuranceNo">
              <el-input v-model="form.insuranceNo" placeholder="请输入医保卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="门诊诊断" prop="diagnosis">
              <el-input v-model="form.clinicDiagnosis" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="建议预交金" prop="paymentAdvice">
              <el-input v-model="form.paymentAdvice" placeholder="请输入建议预交金" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注意事项" prop="remarks">
          <el-input v-model="form.remarks" :rows="2" type="textarea" placeholder="请输入注意事项" />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: "PatVisitIndex"
}
</script>

<script setup lang="ts">
import { addPatVisitIndex, updatePatVisitIndex } from "@/api/hospitalization/patVisitIndex";
import { useUserStoreHook } from "@/store/modules/user";
import { useDictStoreHook } from "@/store/modules/dict";
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('profession_dict', 'medical_insurance_type', 'nationality_dict', 'relationship_dict',
  'medical_insurance_type', 'sys_yes_no', 'sys_user_sex', 'nation_dict', 'pay_way_dict', 'marriage_dict', 'education_dict',
  'admission_situation_dict', 'patient_class_dict'))

const useUserStore = useUserStoreHook();
const { proxy } = getCurrentInstance() as any;
const clinicId = ref(proxy.$clinicId)
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;

const props = defineProps({
  inoroutFlag: {
    type: String,
    default: "",
  },
  orgId: {
    type: String,
    default: "",
  },
});

const formRef = ref();
const open = ref(false);
const form = ref({
  visitIndexId: "",
  visitId: "",
  newbornFlag: "Y",
  name: "",
  inputCode: "",
  height: "",
  weight: 0,
  sex: "",
  marriage: "",
  occupation: "",
  age: 0,
  ageMonth: 0,
  ageDay: 0,
  ageHours: 0,
  ageMinutes: 0,
  nation: "",
  nationality: "",
  canBaoType: "",
  birthdayDate: "",
  nativePlace: "",
  nativePlaceCode: "",
  zipCode: "",
  homePlaceCode: "",
  homePlace: "",
  addressCode: "",
  address: "",
  identity: "",
  idNo: "",
  phone: "",
  contacts: "",
  contactsAddress: "",
  contactsAddressCode: "",
  contactsPhone: "",
  contactsRelation: "",
  unitInContractId: "",
  unitInContract: "",
  workContract: "",
  affiliatingArea: "",
  insuranceType: "",
  isCard: "",
  insuranceNo: "",
  chargeType: "",
  externalCause: "",
  autopsyFlag: "",
  practiceDoctor: "",
  engageDoctor: "",
  isDrugAllerge: "",
  drugAllergy: "",
  orgId: "",
  remarks: "",
  rh: "",
  abo: "",
  pathologicalDiagCode: "",
  pathologicalNo: "",
  pathologicalDiag: "",
  registZipCode: "",
  registAddress: "",
  registAddressCode: "",
  workCode: "",
  workPhone: "",
  externalCauseCode: "",
  paymentAdvice: 0,
  residenceNo: "",
  medicalPayWay: "",
  brainDamageTimeBeforeDay: "",
  brainDamageTimeBeforeHour: "",
  brainDamageTimeBeforeMinute: "",
  brainDamageTimeAfterDay: "",
  brainDamageTimeAfterHour: "",
  brainDamageTimeAfterMinute: "",
  comeAgainIf: "",
  comeAgainPurpose: "",
  persType: "",
  centerId: "",
  eduLevel: "",
  matnType: "",
  latechbFlag: "",
  pretFlag: "",
  birctrlMatnDate: "",
  fpscNo: "",
  birctrlType: "",
  gesoVal: "",
  fetts: "",
  fetusCnt: "",
  localFlag: "",
  createTime: new Date(),
  deptAdmissionTo: "",
  wardAdmissionTo: "",
  hospitalSituation: "",
  channel: "1",
  contactsIdNo: "",
  quarantine: "N",
  clinicDiagnosis: ""
})

const rules = ref({
  visitIndexId: [
    {
      required: true, message: "主键不能为空", trigger: "blur"
    }
  ],
  visitId: [
    {
      required: true, message: "住院id不能为空", trigger: "blur"
    }
  ],
  name: [
    {
      required: true, message: "患者姓名不能为空", trigger: "blur"
    }
  ],
  sex: [
    {
      required: true, message: "性别不能为空", trigger: "change"
    }
  ],
})


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    visitIndexId: "",
    visitId: "",
    newbornFlag: "0",
    name: "",
    inputCode: "",
    height: "",
    weight: 0,
    sex: "",
    marriage: "",
    occupation: "",
    age: 0,
    ageMonth: 0,
    ageDay: 0,
    ageHours: 0,
    ageMinutes: 0,
    nation: "",
    nationality: "",
    canBaoType: "",
    birthdayDate: "",
    nativePlace: "",
    nativePlaceCode: "",
    zipCode: "",
    homePlaceCode: "",
    homePlace: "",
    addressCode: "",
    address: "",
    identity: "",
    idNo: "",
    phone: "",
    contacts: "",
    contactsAddress: "",
    contactsAddressCode: "",
    contactsPhone: "",
    contactsRelation: "",
    unitInContractId: "",
    unitInContract: "",
    workContract: "",
    affiliatingArea: "",
    insuranceType: "",
    isCard: "",
    insuranceNo: "",
    chargeType: "",
    externalCause: "",
    autopsyFlag: "",
    practiceDoctor: "",
    engageDoctor: "",
    isDrugAllerge: "",
    drugAllergy: "",
    orgId: "",
    createBy: "",
    updateBy: "",
    updateTime: "",
    remarks: "",
    rh: "",
    abo: "",
    pathologicalDiagCode: "",
    pathologicalNo: "",
    pathologicalDiag: "",
    registZipCode: "",
    registAddress: "",
    registAddressCode: "",
    workCode: "",
    workPhone: "",
    externalCauseCode: "",
    paymentAdvice: 0,
    residenceNo: "",
    medicalPayWay: "",
    brainDamageTimeBeforeDay: "",
    brainDamageTimeBeforeHour: "",
    brainDamageTimeBeforeMinute: "",
    brainDamageTimeAfterDay: "",
    brainDamageTimeAfterHour: "",
    brainDamageTimeAfterMinute: "",
    comeAgainIf: "0",
    comeAgainPurpose: "",
    persType: "",
    centerId: "",
    eduLevel: "",
    matnType: "",
    latechbFlag: "",
    pretFlag: "",
    birctrlMatnDate: "",
    fpscNo: "",
    birctrlType: "",
    gesoVal: "",
    fetts: "",
    fetusCnt: "",
    localFlag: ""
  };
  nextTick(() => {
    formRef.value.resetFields()
  })
}

function getList() {
  const param = {

  }
}


/**
 * 根据出生日期获取年龄
 */
function birthdayDateChange() {
// 获取年龄 返回年月日小时对象
  const ageObj = proxy.$certificateNoInAge(form.value.birthdayDate);
  if (ageObj) {
    form.value.age = ageObj.yStr;
    form.value.ageMonth = ageObj.mStr;
    form.value.ageDay = ageObj.dStr;
    form.value.ageHours = ageObj.hStr;
  }
}

/** 重置按钮操作 */
function resetQuery() {

}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      if (form.value.visitIndexId != null) {
        updatePatVisitIndex(form.value).then((response: any) => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      }
      else {
        addPatVisitIndex(form.value).then((response: any) => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      }
    }
  });
}

function wordChange(obj) {
  form.value.wardAdmissionTo = obj.wardCode

}

function deptChange(obj) {
  form.value.deptAdmissionTo = obj.deptId
}

// 年龄组清空
function resetAge() {
    form.value.age = 0
    form.value.ageMonth = 0
    form.value.ageDay = 0
    form.value.ageHours = 0
    form.value.ageMinutes = 0
}

getList();
</script>

<style scoped>
:deep(.age .el-input__inner) {
  width: 50px;
}
</style>
